package com.ewin.datepicker;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import com.ewin.datepicker.wheelview.OnWheelScrollListener;
import com.ewin.datepicker.wheelview.WheelView;
import com.ewin.datepicker.wheelview.adapter.NumericWheelAdapter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimePickerPopWindow extends PopupWindow{
	private Context context;
	private String startTime;//传进来默认的时间
	private Date date;
	private int curYear,curMonth,curDay;
	private LayoutInflater mInflater;
	private View dateView;
//	private WheelView yearView;
//	private WheelView monthView;
//	private WheelView dayView;
		private WheelView hourView;
	private WheelView minView;
//	private WheelView secView;
	private int[] timeInt;
	UpDate update;
//	private final int MinYear =1970;
//	private final int MaxYear =2070;
	private boolean isGreater;//isGreater判断是选择将来时间还是过去时间
	public TimePickerPopWindow(Context context, String startTime, boolean isGreater, UpDate update){
		this.context=context;
		this.startTime=startTime;
		this.update=update;
		this.isGreater=isGreater;
		setStartTime();
		initWindow();
	}
	private void setStartTime() {
		if(startTime.isEmpty()){
			SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
			startTime = sdf.format(new Date());
		}
		// TODO Auto-generated method stub
//		startTime格式  "HH:ss"  后面需要时分再加
		timeInt=new int[2];
//		timeInt[0]=Integer.valueOf(startTime.substring(0, 4));
//		timeInt[1]=Integer.valueOf(startTime.substring(5, 7));
//		timeInt[2]=Integer.valueOf(startTime.substring(8,10));
		timeInt[0]=Integer.valueOf(startTime.substring(0,2));
		timeInt[1]=Integer.valueOf(startTime.substring(3,5));
//		timeInt[5]=Integer.valueOf(startTime.substring(12, 14));
	}
	private void initWindow() {
		// TODO Auto-generated method stub
		mInflater=LayoutInflater.from(context);
		dateView=mInflater.inflate(R.layout.wheel_time_picker, null);
//		yearView=(WheelView) dateView.findViewById(R.id.year);
//		monthView=(WheelView) dateView.findViewById(R.id.month);
//		dayView=(WheelView) dateView.findViewById(R.id.day);
		hourView=(WheelView) dateView.findViewById(R.id.time);
		minView=(WheelView) dateView.findViewById(R.id.min);
//		secView=(WheelView) dateView.findViewById(R.id.sec);
//		secView.setVisibility(View.GONE);
		initWheel();
	}
	private void initWheel() {
		// TODO Auto-generated method stub
		Calendar calendar=Calendar.getInstance();
		curYear=calendar.get(Calendar.YEAR);
		curMonth=calendar.get(Calendar.MONTH)+1;
		curDay=calendar.get(Calendar.DAY_OF_MONTH);

//		NumericWheelAdapter numericWheelAdapter1=new NumericWheelAdapter(context,MinYear,MaxYear);
//		numericWheelAdapter1.setLabel("年");
//		yearView.setViewAdapter(numericWheelAdapter1);
//		yearView.setCyclic(true);
//		yearView.addScrollingListener(scrollListener);

//		NumericWheelAdapter numericWheelAdapter2=new NumericWheelAdapter(context,1, 12, "%02d");
//		numericWheelAdapter2.setLabel("月");
//		monthView.setViewAdapter(numericWheelAdapter2);
//		monthView.setCyclic(true);
//		monthView.addScrollingListener(scrollListener);
//
//		NumericWheelAdapter numericWheelAdapter3=new NumericWheelAdapter(context,1, getDay(curYear, curMonth), "%02d");
//		numericWheelAdapter3.setLabel("日");
//		dayView.setViewAdapter(numericWheelAdapter3);
//		dayView.setCyclic(true);
//		dayView.addScrollingListener(scrollListener);

		NumericWheelAdapter numericWheelAdapter4=new NumericWheelAdapter(context,0, 23, "%02d");
		numericWheelAdapter4.setLabel("时");
		hourView.setViewAdapter(numericWheelAdapter4);
		hourView.setCyclic(true);
		hourView.addScrollingListener(scrollListener);

		NumericWheelAdapter numericWheelAdapter5=new NumericWheelAdapter(context,0, 59, "%02d");
		numericWheelAdapter5.setLabel("分");
		minView.setViewAdapter(numericWheelAdapter5);
		minView.setCyclic(true);
		minView.addScrollingListener(scrollListener);
//
//		yearView.setCurrentItem(timeInt[0]-MinYear);
//		monthView.setCurrentItem(timeInt[1]-1);
//		dayView.setCurrentItem(timeInt[2]-1);
		hourView.setCurrentItem(timeInt[0]);
		minView.setCurrentItem(timeInt[1]);
//		yearView.setVisibleItems(7);//设置显示行数
//		monthView.setVisibleItems(7);
//		dayView.setVisibleItems(7);
		hourView.setVisibleItems(7);
		minView.setVisibleItems(7);
		setContentView(dateView);
		setWidth(LayoutParams.FILL_PARENT);
		setHeight(LayoutParams.WRAP_CONTENT);
		ColorDrawable dw = new ColorDrawable(0xFFFFFFFF);
		setBackgroundDrawable(dw);
		setFocusable(true);
	}
	OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
		@Override
		public void onScrollingStarted(WheelView wheel) {

		}

		@Override
		public void onScrollingFinished(WheelView wheel) {
			String birthday=new StringBuilder().append((String.format("%02d", hourView.getCurrentItem()))).append(":").append((String.format("%02d", minView.getCurrentItem()))).toString();
			update.onUpDate(birthday);

		}
	};
	public static interface UpDate{
		void onUpDate(String s);
	}
}
